package com.lxh.student.service.impl;

import com.lxh.student.entity.Teacher;
import com.lxh.student.entity.TeacherDetails;
import com.lxh.student.service.TeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author lxh
 * @date 2023/4/4 21:18
 */

@Service
@Slf4j
public class TeacherDetailServiceImpl implements UserDetailsService {

    @Autowired
    private TeacherService teacherService;
    @Override
    public TeacherDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        log.info("TeacherDetailServiceImpl");
        //1.根据用户名获取数据库中的用户信息
        Teacher teacher=teacherService.findOne(username);
        if(Objects.isNull(teacher)){
            log.info("用户名错误！");
            throw new RuntimeException("用户名错误！");
        }
        log.info("用户名验证成功！");
        //2.TODO 查询权限信息 需要查询真是的数据库 先做一个假的
        List<String> list=new ArrayList<>(Arrays.asList("test1","test2","test3"));

        //3.返回UserDetails
        return new TeacherDetails(teacher,list);
    }
}
